package com.ctgu.springshiro.service;

import com.ctgu.springshiro.dao.ItemKillSuccessDao;
import com.ctgu.springshiro.entity.KillSuccessUserInfo;
import org.apache.activemq.ScheduledMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.*;

@Service
public class ActiveSenderService {

    private final static Logger log = LoggerFactory.getLogger(ActiveSenderService.class);

    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @Resource(name = "delayQueue")
    private Queue delayQueue;

    @Autowired
    private Topic topic;

    @Autowired
    private ItemKillSuccessDao itemKillSuccessDao;

    @Value("${mq.kill.item.success.kill.expire}")
    private long expireTime;

    public void sendMessage(Destination destination, String message) {
        jmsTemplate.convertAndSend(destination, message);
    }

    public void sendMessage(String message) {
        jmsTemplate.convertAndSend(queue, message);
    }

    public void sendKillSuccessEmailMsg(String orderNo) {
        log.info("秒杀成功异步发送邮件通知消息-准备发送消息：{}", orderNo);

        try {
            if (StringUtils.isNotBlank(orderNo)) {
                KillSuccessUserInfo info = itemKillSuccessDao.selectByCode(orderNo);
                if (info != null) {
                    //TODO:rabbitmq发送消息的逻辑
                    jmsTemplate.setDefaultDestination(queue);
                    //TODO：将info充当消息发送至队列
                    jmsTemplate.convertAndSend(info, (message) -> {
                        message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
                        return message;
                    });
                }
            }
        } catch (Exception e) {
            log.error("秒杀成功异步发送邮件通知消息-发生异常，消息为：{}", orderNo, e.fillInStackTrace());
        }
    }

    public void sendKillSuccessOrderExpireMsg(String orderNo) {
        try {
            if (StringUtils.isNotBlank(orderNo)) {
                KillSuccessUserInfo info = itemKillSuccessDao.selectByCode(orderNo);
                if (info != null) {
                    jmsTemplate.setDefaultDestination(delayQueue);
                    //TODO：将info充当消息发送至队列
                    jmsTemplate.convertAndSend(info, (message) -> {
                        message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
                        message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, expireTime);
                        return message;
                    });
                }
            }
        } catch (Exception e) {
            log.error("秒杀成功后生成抢购订单-发送信息入延迟队列，等待着一定时间失效超时未支付的订单-发生异常，消息为：{}", orderNo, e.fillInStackTrace());
        }
    }

    public void publish(String msg) {
        jmsTemplate.convertAndSend(topic, msg);
    }
}